package com.loooooo.pm2024.modules.pm.controller;


import com.loooooo.pm2024.data.Result;
import com.loooooo.pm2024.modules.pm.model.Attachment;
import com.loooooo.pm2024.modules.pm.model.IssueComment;
import com.loooooo.pm2024.modules.pm.selector.AttachmentSelector;
import com.loooooo.pm2024.modules.pm.selector.IssueCommentSelector;
import com.loooooo.pm2024.modules.pm.service.AttachmentService;
import com.loooooo.pm2024.modules.pm.service.IssueCommentService;
import com.loooooo.pm2024.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/pm/attachment")
public class AttachmentController {
    @Autowired
    AttachmentService attachmentService;


    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result listPost(@RequestBody(required = false) AttachmentSelector selector) {
        if (selector == null) selector = new AttachmentSelector();

        if (selector.getPageSize() != null) {
            if (selector.getPageNo() == null || selector.getPageNo() < 1) {
                selector.setPageNo(1);
            }
            Result result = new Result();
            int count = attachmentService.count(selector);
            result.set("total", count);
            if (count > (selector.getPageSize() * (selector.getPageNo() - 1))) {
                result.setData(attachmentService.list(selector));
            }
            return result.set("now", System.currentTimeMillis());
        }
        return Result.success(attachmentService.list(selector)).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    Result update(@ModelAttribute Attachment attachment, @RequestParam("file") MultipartFile multipartFile) {
        int r = attachmentService.update(attachment, multipartFile);
        return Result.success(attachment).set("affect", r).set("now", System.currentTimeMillis());
    }

    @PostMapping(value = "/download")
    @ResponseBody
    void download(@RequestParam("id") Long id, HttpServletResponse response, HttpServletRequest request) {

        Attachment attachment = attachmentService.get(id);
        File file = new File("/image2/pm2024/attachment/", (id + "").substring(0, 2) + "/" + (id + "").substring(2) + "/" + attachment.getFilename());

        try {

            CacheControl cacheControl = CacheControl.maxAge(10, TimeUnit.DAYS);
            response.setHeader(HttpHeaders.CACHE_CONTROL, cacheControl.getHeaderValue());
            response.setContentType(request.getServletContext().getMimeType(file.getName()));
            IOUtils.write(file, response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
